package com.tiancheng.trade.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.exception.error.BusinessErrorInfoEnum;
import com.tiancheng.trade.commom.core.utils.IdWorkerUtils;
import com.tiancheng.trade.commom.web.model.OrgBusinessDTO;
import com.tiancheng.trade.merchant.mapper.OrgBusinessMapper;
import com.tiancheng.trade.merchant.model.Business;
import com.tiancheng.trade.merchant.model.OrgBusiness;
import com.tiancheng.trade.merchant.model.Organization;
import com.tiancheng.trade.merchant.service.IBusinessService;
import com.tiancheng.trade.merchant.service.IOrgBusinessService;
import com.tiancheng.trade.merchant.util.IdGenerateUtil;
import com.tiancheng.trade.merchant.vo.merchant.QueryOrgBusinessReqVO;
import com.tiancheng.trade.merchant.vo.merchant.QueryOrgBusinessResVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @Author: kellen
 * @Description: 机构业务管理 service impl
 * @Date: create in 2024/11/19 14:02
 */
@Service
public class OrgBusinessServiceImpl extends ServiceImpl<OrgBusinessMapper, OrgBusiness> implements IOrgBusinessService {
    @Resource
    private OrganizationService organizationService;
    @Resource
    private IBusinessService businessService;
    @Resource
    private IdGenerateUtil idGenerateUtil;

    @Override
    public void newOrgBusiness(OrgBusiness orgBusiness) {
        /*final List<OrgBusiness> list = this.list(new LambdaQueryWrapper<OrgBusiness>().eq(OrgBusiness::getOrganizationCode, orgBusiness.getOrganizationCode()).eq(OrgBusiness::getBusinessCode, orgBusiness.getBusinessCode()));
        BusinessErrorInfoEnum.SYSTEM_ERROR.assertIsTrue(CollectionUtils.isEmpty(list), "机构已授权授权改业务，请不要重复操作");*/
        // 机构校验
        final Organization organization = organizationService.getOne(new LambdaQueryWrapper<Organization>().eq(Organization::getOrganizationCode, orgBusiness.getOrganizationCode()));
        BusinessErrorInfoEnum.SYSTEM_ERROR.assertIsTrue(Objects.nonNull(organization) && Objects.nonNull(organization.getId()), "未查询到机构信息！");
        // 业务校验
        final Business business = businessService.getOne(new LambdaQueryWrapper<Business>().eq(Business::getBusinessCode, orgBusiness.getBusinessCode()));
        BusinessErrorInfoEnum.SYSTEM_ERROR.assertIsTrue(Objects.nonNull(business) && Objects.nonNull(business.getId()), "未查询到业务信息！");
        orgBusiness.setCreatedDt(LocalDateTime.now());
        orgBusiness.setTermNo(idGenerateUtil.getTermNo());
        this.save(orgBusiness);
    }

    @Override
    public List<OrgBusinessDTO> getByClientId(String clientId,String termNo) {
        return this.baseMapper.getByClientId(clientId,termNo);
    }

    @Override
    public PageInfo<QueryOrgBusinessResVO> queryPage(QueryOrgBusinessReqVO conditions) {
        Page<OrgBusiness> page = new Page<>(conditions.getCurrentPage(), conditions.getPageSize());
        final IPage<QueryOrgBusinessResVO> queryOrgBusinessResVOIPage = this.baseMapper.queryPage(page, conditions);
        return new PageInfo<>(queryOrgBusinessResVOIPage.getRecords(), new PageInfo.Pagination(queryOrgBusinessResVOIPage.getCurrent(),
                (int) queryOrgBusinessResVOIPage.getSize(), queryOrgBusinessResVOIPage.getTotal()));
    }

    @Override
    public List<QueryOrgBusinessResVO> queryList(QueryOrgBusinessReqVO conditions) {
        return this.baseMapper.queryList(conditions);
    }

    @Override
    public void saveOrgBusiness(OrgBusiness business) {
        if (null != business.getId()) {
            // 修改
            // 直接删除掉缓存的application信息
            this.updateById(business);
        } else {
            final long count = this.count(new LambdaQueryWrapper<OrgBusiness>().eq(OrgBusiness::getOrganizationCode, business.getOrganizationCode())
                    .eq(OrgBusiness::getBusinessCode, business.getBusinessCode()));
            BusinessErrorInfoEnum.SYSTEM_ERROR.assertIsTrue(count==0,"同一个机构不能重复添加相同业务");
            business.setCreatedDt(LocalDateTime.now());
            this.save(business);
        }
    }

    @Override
    public void delete(Long id) {
        final OrgBusiness orgBusiness = this.getById(id);
        BusinessErrorInfoEnum.SYSTEM_ERROR.assertIsTrue(null!=orgBusiness,"未查询到机构业务配置");
        orgBusiness.setIsDelete(1);
        orgBusiness.setLastUpdDt(LocalDateTime.now());
        this.updateById(orgBusiness);
    }
}
